package priv.ljh.operate.controller;

import com.alibaba.druid.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import priv.ljh.operate.entity.User;
import priv.ljh.operate.mapper.UserMapper;
import priv.ljh.operate.service.UserService;

import java.util.ArrayList;
import java.util.List;

import static priv.ljh.utils.Constants.MESSAGE_FALL;
import static priv.ljh.utils.Constants.STATUS_OK;
/**
 * @author ：Yshuaong
 * @date ：Created in 2021/6/14 14:06
 * @description：
 * @modified By：
 * @version: $
 */
@Api(tags = {"会员"})
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("注册")
    @GetMapping
    @RequestMapping("/register")
    public int register(@RequestParam("phone") String phone, @RequestParam("password") String password){
        if (StringUtils.isEmpty(phone)){
            System.out.println("手机号码不允许为空");
            return 0;
        }
        if (StringUtils.isEmpty(password)){
            System.out.println("密码不允许为空");
            return 0;
        }

        User user = userService.selectUser(phone);
        if (user!=null){
            System.out.println("手机号码已存在");
            return 0;
        }
        int count = userService.register(phone,password);
        System.out.println("注册成功");
        return 1;
    }
    private boolean flag=false;
    public String phone;
    public String password;
    @ApiOperation("登录")
    @GetMapping
    @RequestMapping("/login")
    public List login(@RequestParam("phone") String phone, @RequestParam("password") String password){

        if (flag==true){
            List<String> list = new ArrayList<String>();
            list.add(String.valueOf(flag));
            list.add("http://localhost:8080/#/");
            return list;
        }
        if (StringUtils.isEmpty(phone)){
            List<String> list = new ArrayList<String>();
            list.add("手机号码不允许为空");
            return list;
        }

        if (StringUtils.isEmpty(password)){
            List<String> list = new ArrayList<String>();
            list.add("密码不允许为空");
            return list;
        }
        User user = userService.selectUser(phone);
        String str= String.valueOf(user);
        if (user==null){
            List<String> list = new ArrayList<String>();
            list.add("没有此用户");
            System.out.println("没有此用户");
            return list;
        }
        if (!password.equals(user.getPassword())){
            List<String> list = new ArrayList<String>();
            list.add("密码错误");
            System.out.println("密码错误");
            return list;
        }
        flag=true;
        this.phone=phone;
        this.password=password;
        List<String> list=new ArrayList<>();
        list.add("登录成功");
        list.add(phone);
        list.add(password);
        list.add(String.valueOf(flag));
        System.out.println("登录成功");
        return list;
    }

    @ApiOperation("获取登录状态")
    @GetMapping
    @RequestMapping("/getFlag")
    public boolean getFlag(){
        return flag;
    }
    @ApiOperation("获取用户信息")
    @GetMapping
    @RequestMapping("/getUserInfo")
    public List getUserInfo(){
        List list=new ArrayList();
        if (flag==true){
            System.out.println("222s");
            int uid=userService.seleteId(phone);
            int seller=userService.seleteSeller(phone);
            list.add(phone);
            list.add(uid);
            list.add(password);
            list.add(seller);
            return list;
        }else{
            System.out.println("11111111111");
            list.add("fail");
            return list;
        }

    }


    @ApiOperation("申请商家")
    @GetMapping
    @RequestMapping("/setSeller")
    public String setSeller(@RequestParam("username") String username,
                            @RequestParam("identity") String identity,
                            @RequestParam("id") int id){
        int x=userService.setSeller(username, identity,id);
        if (x!=0){
            return "注册商家成功";
        }else{
            return "注册商家失败";
        }
    }

    @ApiOperation("updatePassword")
    @GetMapping
    @RequestMapping("/updatePassword")
    public String updatePassword(@RequestParam("phone") String phone,
                            @RequestParam("password") String password){
        int x=userService.updatePassword(password,phone);
        if (x!=0){
            return "修改密码成功";
        }else{
            return "修改密码失败";
        }
    }

    @ApiOperation("登出")
    @GetMapping
    @RequestMapping("/logout")
    public boolean logout(){
        flag=false;
        return flag;
    }

}